<?php

include_once S_ROOT . '/include/class/db/DBInterface.class.php';

/**
 * 数据库类
 */
class DB {

    private $link = null;

    public function getConection($dsn) {
        $dsn = parse_url($dsn);
        switch (strtolower($dsn['scheme'])) {
            case 'sqlite':
                include_once S_ROOT . '/include/class/db/driver/SQLite.class.php';
                $this->link = new SQLite();
                break;
            default :
                include_once S_ROOT . '/include/class/db/driver/Mysql.class.php';
                $this->link = new Mysql();
        }
        $this->link->getConection($dsn);
    }

    public function query($sql) {
        return $this->link->query($sql);
    }

    public function fetchArray($result) {
        return $this->link->fetchArray($result);
    }

    /**
     * 想数据库中插入表
     * @param string $table
     * @param array $insertField
     * @param bool $replace
     */
    public function insertTable($table, $insertField, $replace=false) {
        $f = $fv = array();
        foreach ($insertField as $k => $v) {
            $f[] = $k;
            $fv[] = '\'' . $v . '\'';
        }
        $sql = ($replace ? 'REPLACE' : 'INSERT') . " INTO " . Utils::tname($table) . "(" . implode(',', $f) . ")VALUE(" . implode(',', $fv) . ")";
        return $this->query($sql);
    }

    /**
     * 更新表
     * @param string $table 表名
     * @param array $updatefields 要更新的字段和其值
     * @param array $condition 条件
     * @param string $op 条件操作符
     */
    public function updateTable($table, $updatefields, $condition, $op='=') {
        $sql = "UPDATE " . Utils::tname($table) . " SET ";
        $tmp = array();
        foreach ($updatefields as $k => $v) {
            $tmp[] = $k . "='$v'";
        }
        $sql.=' ' . implode(',', $tmp);

        $tmp = array();
        foreach ($condition as $k => $v) {
            $tmp[] = $k . $op . "'$v'";
        }
        $sql.=' WHERE ' . implode(' AND ', $tmp);
        return $this->query($sql);
    }

    /**
     * 从表中删除数据
     * @param string $table 表名
     * @param array $condition 条件
     * @param string $op 条件操作符
     */
    public function delete($table, $condition, $op='=') {
        $sql = 'DELETE FROM ' . Utils::tname($table) . ' WHERE ';
        $tmp = array();
        foreach ($condition as $k => $v) {
            $tmp[] = $k . $op . "'$v'";
        }
        $sql.=implode(' AND ', $tmp);
        return $this->query($sql);
    }

    /**
     * 取出表中记录集的条数
     * @param string $table 表名
     * @param array $condition 条件
     * @param string $op 条件操作符
     */
    public function getCount($table, $condition, $op='=') {
        $sql = "SELECT COUNT(*) AS num FROM " . Utils::tname($table);
        $tmp = array();
        foreach ($condition as $k => $v) {
            $tmp[] = $k . $op . "'$v'";
        }
        $sql.=' WHERE ' . implode(' AND ', $tmp);
        $result = $this->fetchArray($this->query($sql));
        return $result['num'];
    }

    /**
     * 从表中取出一条记录
     * @param string $table 表名
     * @param array $condition 条件
     * @param string $op 条件操作符
     */
    public function getOne($table, $condition, $op='=') {
        $sql = "SELECT * FROM " . Utils::tname($table);
        $tmp = array();
        foreach ($condition as $k => $v) {
            $tmp[] = $k . $op . "'$v'";
        }
        $sql.=' WHERE ' . implode(' AND ', $tmp).' LIMIT 1';

        return $this->fetchArray($this->query($sql));
    }

    public function getVersion() {
        return $this->link->version();
    }

}

class DBExcepton extends Exception {

    public function __construct($message) {
        parent::__construct($message);
    }

}

?>
